package com.endurance.advice;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class AnnotationAdvice {

    @Component
    @Aspect
    public class ServiceAdvice02 {

        @Pointcut("execution(* com.endurance.service.impl.ProductServiceImpl.find()))")
        public void pointcut() {

        }

        @Before("pointcut()")
        public void beforeAdvice(JoinPoint joinPoint) {
            System.out.println("前置通知...");
        }

        @AfterReturning(pointcut = "pointcut()", returning = "retValue")
        public void afaterAdvice(JoinPoint joinPoint, Object retValue) {
            System.out.println("后置通知...");
        }

        @After("pointcut()")
        public void finallyAdvice() {
            System.out.println("最终通知...");
        }

        @AfterThrowing(pointcut = "pointcut()", throwing = "throwable")
        public void afterThrowingAdvice(JoinPoint joinPoint, Throwable throwable) {
            System.out.println("异常通知...");
        }

        @Around("pointcut()")
        public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
            try {
                System.out.println("环绕通知-前置通知...");
                proceedingJoinPoint.proceed();
                System.out.println("环绕通知-后置通知...");
            } catch (Throwable e) {
                System.out.println("环绕通知-异常通知...");
                throw new RuntimeException(e);
            } finally {
                System.out.println("环绕通知-最终通知...");
            }
        }
    }

}
